﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;

namespace shopform
{
    public class Company : Validateable
    {
        public String CompanyID;
        public String CompanyBusinessNumber;
        public String CompanyName;
        public String CompanyCountry;
        public String CompanyCity;
        public String CompanyAddr;
        public String CompanyPostCode;
        public int CompanyRequestedLanguage;

        /// <summary>
        /// Empty constructor for model Company.
        /// </summary>
        public Company()
        {
        }

        /// <summary>
        /// Sets fields according to Company ID.
        /// </summary>
        /// <param name="cID">Company ID</param>
        public Company(String cID)
        {
            try
            {
                String q = "SELECT * FROM Companies " +
                           "WHERE CompanyID = '" + cID + "';";
                DataTable dt = myDatabaseCommander.ExecuteArray(q);

                foreach (DataRow r in dt.Rows)
                {
                    this.CompanyID = r["CompanyID"].ToString();
                    this.CompanyAddr = r["CompanyAddr"].ToString();
                    this.CompanyBusinessNumber = r["CompanyBusinessNumber"].ToString();
                    this.CompanyCity = r["CompanyCity"].ToString();
                    this.CompanyCountry = r["CompanyCountry"].ToString();
                    this.CompanyName = r["CompanyName"].ToString();
                    this.CompanyPostCode = r["CompanyPostCode"].ToString();
                    this.CompanyRequestedLanguage = 
                        (int) Int32.Parse(r["CompanyRequestedLanguage"].ToString());
                }
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Get all company information in a data table.
        /// </summary>
        /// <returns>Data Table with all company information.</returns>
        public static DataTable getAllCompaniesForSelection()
        {
            String query;
            query = "SELECT CompanyName, CompanyID, CompanyBusinessNumber, " +
                    "CompanyCountry, CompanyCity, LanguageName " +
                    "FROM Companies, Languages " +
                    "WHERE CompanyRequestedLanguage = LanguageID " + 
                    "ORDER BY CompanyName ASC;";
            return (myDatabaseCommander.ExecuteArray(query));
        }

        
        /// <summary>
        /// Get all company information in a data table with name like parameter.
        /// </summary>
        /// <param name="sName">Company name to search for.</param>
        /// <returns>DataTable with results.</returns>
        public static DataTable getAllCompaniesWithNameLike(String sName)
        {
            String query;
            query = "SELECT CompanyName, CompanyID, CompanyBusinessNumber, " +
                    "CompanyCountry, CompanyCity, LanguageName " +
                    "FROM Companies, Languages " +
                    "WHERE CompanyRequestedLanguage = LanguageID AND " +
                    "CompanyName LIKE '%" + sName + "%' " +
                    "ORDER BY CompanyName ASC;";
            return (myDatabaseCommander.ExecuteArray(query));
        }

        /// <summary>
        /// Inserts Company data into database.
        /// Checks for valid data.
        /// </summary>
        public void insertCompany()
        {
            try
            {
                this.isValid();
                String query;
                query = "INSERT INTO " +
                           "Companies(CompanyID, CompanyBusinessNumber, " +
                                       "CompanyName, CompanyCountry, CompanyCity, " +
                                       "CompanyAddr, CompanyPostCode, CompanyRequestedLanguage) " +
                           "VALUES ('" + this.CompanyID + "', " +
                                   "'" + this.CompanyBusinessNumber + "', " +
                                   "'" + this.CompanyName + "', " +
                                   "'" + this.CompanyCountry + "', " +
                                   "'" + this.CompanyCity + "', " +
                                   "'" + this.CompanyAddr + "', " +
                                   "'" + this.CompanyPostCode + "', " +
                                   this.CompanyRequestedLanguage + ");";
                
                myDatabaseCommander.ExecuteNonQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Updates selected company.
        /// </summary>
        public void updateCompany()
        {
            try
            {
                String query;
                query = "UPDATE Companies SET " +
                        "CompanyID = '" + this.CompanyID + "', " +
                        "CompanyName = '" + this.CompanyName + "', " +
                        "CompanyBusinessNumber = '" + this.CompanyBusinessNumber + "', " +
                        "CompanyAddr = '" + this.CompanyAddr + "', " +
                        "CompanyCity = '" + this.CompanyCity + "', " +
                        "CompanyCountry = '" + this.CompanyCountry + "', " +
                        "CompanyPostCode = '" + this.CompanyPostCode + "', " +
                        "CompanyRequestedLanguage = " + this.CompanyRequestedLanguage + " " +
                        "WHERE CompanyID = '" + this.CompanyID + "';";

                myDatabaseCommander.ExecuteNonQuery(query);
            }
            catch (Exception e)
            {
                throw e;
            }
        }

        /// <summary>
        /// Checks for valid data, returns boolean.
        /// </summary>
        /// <returns>Valid data status</returns>
        public bool isValid()
        {
            return true;
        }

    }//class
}//namespaces
